<?php
namespace Api\Controller;
use Think\Controller;
class HomeController extends CommonController{

        public function allService() {

            //服务区域
            $serviceList = $this->regionModel->getOpenRegionList();

            $this->returnData['data']['region_list'] = $serviceList;

            $this->response($this->returnData, 'json');

        }

        public function index() {

            $latitude = I("post.latitude");
            $longitude = I("post.longitude");
            $token = I("post.token");

            $userMap['token'] = $token;
            $user = $this->userModel->where($userMap)->find();

            if(!isNullOrEmpty($user)) {

                if(!isNullOrEmpty($latitude)
                    && !isNullOrEmpty($longitude)) {
                    $user["latitude"] = $latitude;
                    $user["longitude"] = $longitude;
                    $user["geohash"] = $this->geohash->encode($latitude, $longitude);
                    $this->userModel->updateObj($user);
                }

            }

            $bannerList = $this->bannerModel->getAll();

            $recommendList = $this->userModel->getHotMasterList();

            $this->returnData['data']['banner_list'] = $bannerList;
            $this->returnData['data']['recommend_list'] = $recommendList;

            $this->response($this->returnData, 'json');

        }

        public function search() {

            $pageNum = I('post.pageNum') ? I('post.pageNum') : C('DEFAULT_PAGE');

            $list = $this->userModel->searchMasterByCondition($_POST, $pageNum);
            //服务区域
            $serviceList = $this->regionModel->getOpenRegionList();

            $this->returnData['data']['master_list'] = $list;
            $this->returnData['data']['region_list'] = $serviceList;

            $this->response($this->returnData, 'json');

        }


        public function center() {

            //判断用户是否登录
            $token = I("post.token");

            $currentUser = $this->userModel->queryByToken($token);

            if(isNullOrEmpty($currentUser)) {
                $this->errorReturn("ERROR_TOKEN");
            }

            $latitude = I("post.latitude");
            $longitude = I("post.longitude");

            if(!isNullOrEmpty($latitude)
                && !isNullOrEmpty($longitude)) {
                $user["latitude"] = $latitude;
                $user["longitude"] = $longitude;
                $user["geohash"] = $this->geohash->encode($latitude, $longitude);
                $this->userModel->updateObj($user);
            }

            //用户基本信息
            $basicInfo["headimgurl"] = $currentUser["headimgurl"];
            $basicInfo["nickname"] = $currentUser["nickname"];
            $basicInfo["sign"] = $currentUser["sign"];
            $basicInfo["order_number"] = $currentUser["order_number"];
            $basicInfo["is_master"] = $currentUser["is_master"];

            //判断是否有正在审核的申请
            $existMasterApply = $this->masterApplyModel->queryByUserId($currentUser["user_id"]);
            $basicInfo["is_has_apply"] = !isNullOrEmpty($existMasterApply["apply_id"]);

            $this->returnData['data']['basic_info'] = $basicInfo;

            //订单状态数量
            $queryMap["o.user_id"] = $currentUser["user_id"];

            $queryMap["o.status"] = C("ORDER_STATUS.WAITING_CONFIRM");
            $orderInfo["waiting_confirm_number"] = $this->orderModel->getListCount($queryMap);

            $queryMap["o.status"] = C("ORDER_STATUS.WAITING_PAY");
            $orderInfo["waiting_pay_number"] = $this->orderModel->getListCount($queryMap);

            $queryMap["o.status"] = C("ORDER_STATUS.WAITING_SERVICE");
            $orderInfo["waiting_service_number"] = $this->orderModel->getListCount($queryMap);

            $queryMap["o.status"] = C("ORDER_STATUS.END_SERVICE");
            $orderInfo["waiting_comment_number"] = $this->orderModel->getListCount($queryMap);

            $queryMap["o.status"] = C("ORDER_STATUS.WAITING_REFUND");
            $orderInfo["waiting_refund_number"] = $this->orderModel->getListCount($queryMap);

            $this->returnData['data']['order_info'] = $orderInfo;

            $this->response($this->returnData, 'json');

        }

}